﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Portal.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        protected void Application_Start()
        {
            OnStart();

            InitSessionBuilder();
        }

        protected void Application_End(object sender, EventArgs e)
        {
            //Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!");
            OnEnd();
            //下面的代码是关键，可解决IIS应用程序池自动回收的问题
            //Thread.Sleep(1000);
            ////这里设置你的web地址，可以随便指向你的任意一个aspx页面甚至不存在的页面，目的是要激发Application_Start            
            //const string url = "http://localhost:8031/default.aspx";
            //var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            //var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            //var receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流


        }
        protected void Application_Error(object sender, EventArgs e)
        {
            if (Server.GetLastError() == null) return;
            var ex = Server.GetLastError().GetBaseException();
            var sb = new StringBuilder();
            sb.Append(ex.Message);
            sb.Append("\r\nSOURCE: " + ex.Source);
            sb.Append("\r\nFORM: " + Request.Form);
            sb.Append("\r\nQUERYSTRING: " + Request.QueryString);
            sb.Append("\r\n引发当前异常的原因: " + ex.TargetSite);
            sb.Append("\r\n堆栈跟踪: " + ex.StackTrace);

        }
        private void InitSessionBuilder()
        {
            SessionBuilder.SessionStorage = new HttpSessionStorage();
        }

        public static void OnStart()
        {
            Bootstrapper.Run();

        }

        /// <summary>
        /// 结束后台任务
        /// </summary>
        public static void OnEnd()
        {
            IoC.Reset();
        }

        //protected void Application_AcquireRequestState(object sender, EventArgs e)
        //{
        //    if (HttpContext.Current.Session != null)
        //    {

        //        HttpContextBase contextWrapper = new HttpContextWrapper(HttpContext.Current);
        //        RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper);
        //        object culture;
        //        if (routeData != null) routeData.Values.TryGetValue("culture", out culture);
        //        else
        //        {
        //            culture = "zh-cn";
        //        }
        //        var cultureInfo = new CultureInfo(culture.ToString());
        //        // 判断Session中是否有值，没有就设置默认值
        //        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
        //        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
        //        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
        //        System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
        //    }
        //}
    }
}